import { useMemo } from 'react';
import { ManageCommonAPI } from '@/pages/type';
import { getBaseUrl } from '@/services/urlconst';
import { getLoadMapList, getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { getSourceTableQuery } from '../mconfig';

const baseUrl = getBaseUrl();

/**获取寻源管理数据*/
export const SourceModalTableServices = (curRes: Record<string, any>) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    // `${getTenantID()}${ILoadMapTypeAll.COMPANY_TYPE}`,
  ];

  /** 获取数据字典loadMdicMap */
  const { loadMapInfo, mapLoading } = getLoadMapList(loadMdicIds);

  /** 获取权限参数 */
  const queryObj: Record<string, any> = getSourceTableQuery(curRes);

  const { loading, run, refresh, tableProps, data } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        isBlack: 0,
        ...queryObj,
        ...opt,
      };

      const requestURL = `${baseUrl}/supplierInfoController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**刷新管理页面方法*/
  const getSourceTableDateGrid = (resObj: ManageCommonAPI.IGridRequestType) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...resObj,
    };
    run({ ...request });
  };

  return {
    getSourceTableDateGrid,
    refreshSourceDataGrid: refresh,
    sourceDataGridloading: loading,
    sourcePagination: tableProps.pagination,
    sourceDataGrid: data?.rows,
    mapLoading,
    loadMapInfo,
  };
};

/**详情接口*/
export const getBidService = (callback: (res: any) => void) => {
  const getBid = useRequest(() => {}, { manual: true, onSuccess: callback });
  return {
    getBidRun: getBid?.run,
    getBidLoading: getBid?.loading,
    getBidInfo: getBid?.data?.obj,
  };
};

/** 查看/获取意向公开 */
export const GetDetailDisclosureServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };

      let reqUrl = `${baseUrl}/purchaseSourceIntentController/getIntentById/${bodyParams?.id}/${getUserID()}`;
      return requestFunComponFiex('GET', reqUrl);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    GetDetailDisclosureRun: request?.run,
    GetDetailDisclosureLoading: request?.loading,
    reloadDetailInfo: request?.refresh,
  };
};

/**
 * 查看当前流程进度
 * @controller 业务controller
 * @returns
 */
export const getImageByProcInstIDService = (callBack: (res: any) => void) => {
  const getImageByProcInstID = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/purchaseSourceIntentController/getImageByProcInstID/${params?.procInstID}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: callBack,
    },
  );

  const data = useMemo(() => {
    return getImageByProcInstID?.data;
  }, [getImageByProcInstID?.data]);

  return {
    getImageByProcInstIDRun: getImageByProcInstID?.run,
    getImageByProcInstIDLoading: getImageByProcInstID?.loading,
    getImageByProcInstIDData: data,
  };
};

/**同步PDF*/
export const syncPDFtoOAServices = (cb: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };
      let reqUrl = `${baseUrl}/purchaseSourceIntentController/syncWorkFlow`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    syncPDFtoOARun: request?.run,
    syncPDFtoOALoading: request?.loading,
  };
};
